package avicit.im.lantop.basic.companycorrespondence.rest;


import avicit.im.lantop.basic.companycorrespondence.service.CompanyCorrespondenceService;
import avicit.im.lantop.basic.companycorrespondence.service.api.CompanyCorrespondenceServiceApi;
import avicit.im.lantop.userauthset.rest.UserAuthIntfsServiceRest;
import avicit.platform6.core.dao.Paging;
import avicit.platform6.core.jdbc.Page;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@Api(tags = "CompanyCorrespondenceService", description = "CompanyCorrespondenceService")
@RequestMapping("/api/avicit/im/lantop/basic/companycorrespondence/rest/CompanyCorrespondenceServiceRest")
public class CompanyCorrespondenceServiceRest {

    private static final Logger LOGGER = LoggerFactory.getLogger(CompanyCorrespondenceServiceRest.class);


    @Autowired
    private CompanyCorrespondenceService companyCorrespondenceService;


    /**
     * 查询
     * @param map
     * @return
     */
    @PostMapping("/getUserIdsAuthDatasByRecord")
    @ApiOperation(value = "查询")
    public ResponseMsg<Page<Map<String, Object>>> getUserIdsAuthDatasByRecord(@ApiParam(value = "查询", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<Page<Map<String, Object>>> responseMsg = new ResponseMsg<>();
        try {
            Paging<Map<String, Object>> page = (Paging<Map<String, Object>>) map.get("page");
            Map<String, Object> parameter = (Map<String, Object>) map.get("parameter");
            Page<Map<String, Object>> mapPage =  companyCorrespondenceService.loadCompanyCorrespondence(page, parameter);
            responseMsg.setResponseBody(mapPage);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    /**
     * 删除
     * @param map
     * @return
     */
    @PostMapping("/deleteCompanyCorrespondence")
    @ApiOperation(value = "删除")
    public ResponseMsg deleteCompanyCorrespondence(@ApiParam(value = "删除", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<Page<Map<String, Object>>> responseMsg = new ResponseMsg<>();
        try {
            String ids = (String) map.get("ids");
            companyCorrespondenceService.deleteCompanyCorrespondence(ids);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    /**
     * 获取部门
     * @param map
     * @return
     */
    @PostMapping("/getDept")
    @ApiOperation(value = "获取部门")
    public ResponseMsg<Map<String, Object>> getDept(@ApiParam(value = "获取部门", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
        try {

            String orgId = (String) map.get("orgId");
            String orgCode = (String) map.get("orgCode");
            String inputWord = (String) map.get("inputWord");
            Map<String, Object> dept = companyCorrespondenceService.getDept(orgId, orgCode, inputWord);
            responseMsg.setResponseBody(dept);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    /**
     * 查询
     * @param map
     * @return
     */
    @PostMapping("/queryCheckData")
    @ApiOperation(value = "查询")
    public ResponseMsg<Boolean> queryCheckData(@ApiParam(value = "查询", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        try {
            String currentDeptId = (String) map.get("currentDeptId");
            String deptName = (String) map.get("deptName");
            Boolean aBoolean = companyCorrespondenceService.queryCheckData(currentDeptId, deptName);
            responseMsg.setResponseBody(aBoolean);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    /**
     * 新增
     * @param map
     * @return
     */
    @PostMapping("/insertCompanyCorrespondence")
    @ApiOperation(value = "新增")
    public ResponseMsg insertCompanyCorrespondence(@ApiParam(value = "新增", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg responseMsg = new ResponseMsg<>();
        try {
            String orgId = (String) map.get("orgId");
            String currentDeptId = (String) map.get("currentDeptId");
            String currentDeptName = (String) map.get("currentDeptName");
            String deptName = (String) map.get("deptName");
            companyCorrespondenceService.insertCompanyCorrespondence(orgId, currentDeptId, currentDeptName, deptName);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    /**
     * 新增页面，选择现有部门时，需要根据已保存的对应关系，再剔除旧部门中已对应的部门
     * @param map
     * @return
     */
    @PostMapping("/getOldDept")
    @ApiOperation(value = "新增页面，选择现有部门时，需要根据已保存的对应关系，再剔除旧部门中已对应的部门")
    public ResponseMsg<Map<String, Object>> getOldDept(@ApiParam(value = "新增页面，选择现有部门时，需要根据已保存的对应关系，再剔除旧部门中已对应的部门", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
        try {
            String inputWord = (String) map.get("inputWord");
            String newDeptName = (String) map.get("newDeptName");
            String orgId = (String) map.get("orgId");
            String orgCode = (String) map.get("orgCode");
            Map<String, Object> oldDept = companyCorrespondenceService.getOldDept(inputWord, newDeptName, orgId, orgCode);
            responseMsg.setResponseBody(oldDept);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

}
